﻿namespace WMS6.Web.OrderManagement
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.OrderMgtServiceRef;
    using WMS6.Web.UserControls;

    public class OnLineSO2List : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnFinish;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnRefresh;
        protected Button btnReport;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F3051;
        protected GridView gvSalesOrder;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRtnValue;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblSalesOrder;
        protected LinkButton lnkbtnSalesOrderSubmit;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected HtmlTableCell tdlnkbtnSalesOrderSubmit;
        protected UpdatePanel up1;

        private void BindData2SalesOrderList(SalesOrderQueryEntity salesOrderQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(salesOrderQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = OrderMgtServiceClientProxy.GetInstance().GetSalesOrderByQueryEntity(salesOrderQuery);
            base.BindData2GridView(this.gvSalesOrder, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvSalesOrder, "ORDER_ID");
                List<string> listStatus = base.GetSelectedIDsInGridView(this.gvSalesOrder, "STATUS");
                if ((listStatus != null) || (listStatus.Count != 0))
                {
                    for (int i = 0; i < listStatus.Count; i++)
                    {
                        if (listStatus[i].ToString().ToUpper() != "NEW")
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("F3100_006");
                            return;
                        }
                    }
                }
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C008");
                }
                else
                {
                    this.lblMsg.Text = OrderMgtServiceClientProxy.GetInstance().DeleteSalesOrderInfo(selectedID) + base.GetGlobalMsgResource("C006");
                    SalesOrderQueryEntity salesOrderQuery = this.GetSalesOrderQueryEntity();
                    this.BindData2SalesOrderList(salesOrderQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvSalesOrder, "ORDER_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C008");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C009");
            }
            else
            {
                base.Response.Redirect(string.Format("OnLineSO2.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            SalesOrderQueryEntity salesOrderQuery = this.GetSalesOrderQueryEntity();
            salesOrderQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = salesOrderQuery.CurrentPage;
            salesOrderQuery.CurrentPage = "0";
            this.gvSalesOrder.Columns[1].Visible = false;
            this.gvSalesOrder.AllowSorting = false;
            this.BindData2SalesOrderList(salesOrderQuery);
            this.gvSalesOrder.ShowFooter = false;
            salesOrderQuery.IsGetAll = false;
            salesOrderQuery.CurrentPage = currentPageIndex;
            this.SetSalesOrderQueryEntity(salesOrderQuery);
            this.hdnActionID.Value = "";
            base.Export2Excel("OnLineSO.xls", this.gvSalesOrder);
        }

        protected void btnFinish_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvSalesOrder, "ORDER_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C008");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C009");
            }
            else
            {
                this.hdnRtnValue.Value = selectedID[0];
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            SalesOrderQueryEntity salesOrderQuery = this.GetSalesOrderQueryEntity();
            salesOrderQuery.CurrentPage = "0";
            this.BindData2SalesOrderList(salesOrderQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            SalesOrderQueryEntity salesOrderQuery = this.GetSalesOrderQueryEntity();
            salesOrderQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2SalesOrderList(salesOrderQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            SalesOrderQueryEntity salesOrderQuery = this.GetSalesOrderQueryEntity();
            salesOrderQuery.OrderID = "";
            salesOrderQuery.OwnerID = "";
            salesOrderQuery.ConsigneeID = "";
            salesOrderQuery.OrderDate = "";
            salesOrderQuery.RequestedDeliveryDate = "";
            salesOrderQuery.Type = "";
            salesOrderQuery.ExternOrderID = "";
            salesOrderQuery.Status = "";
            salesOrderQuery.CreatedBy = base.GetParamsString(base.UserInfo.UserID, "Same As");
            this.BindData2SalesOrderList(salesOrderQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            SalesOrderQueryEntity salesOrderQuery = this.GetSalesOrderQueryEntity();
            if (int.Parse(salesOrderQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                salesOrderQuery.CurrentPage = (int.Parse(salesOrderQuery.CurrentPage) + 1).ToString();
                this.BindData2SalesOrderList(salesOrderQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            SalesOrderQueryEntity salesOrderQuery = this.GetSalesOrderQueryEntity();
            if (int.Parse(salesOrderQuery.CurrentPage) > 0)
            {
                salesOrderQuery.CurrentPage = (int.Parse(salesOrderQuery.CurrentPage) - 1).ToString();
                this.BindData2SalesOrderList(salesOrderQuery);
            }
        }

        private SalesOrderQueryEntity GetSalesOrderQueryEntity()
        {
            return (base.PageQueryEntitySession as SalesOrderQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                SalesOrderQueryEntity salesOrderQuery = this.GetSalesOrderQueryEntity();
                salesOrderQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2SalesOrderList(salesOrderQuery);
            }
        }

        protected void gvSalesOrder_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
                base.BindingOrdersPriority((Image) e.Row.FindControl("imgPriority"), (Label) e.Row.FindControl("lblPriority"), this.gvSalesOrder.DataKeys[e.Row.RowIndex]["PRIORITY"].ToString(), (string) this.ViewState["PriorityFlag"]);
            }
        }

        protected void gvSalesOrder_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string strValue = base.UrlReplace(this.gvSalesOrder.DataKeys[e.NewSelectedIndex]["ORDER_ID"].ToString());
            if (!string.IsNullOrEmpty(strValue))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'> window.returnValue ='" + strValue + "';  window.close();</script>", false);
            }
        }

        protected void gvSalesOrder_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                SalesOrderQueryEntity salesOrderQuery = this.GetSalesOrderQueryEntity();
                salesOrderQuery.SortField = e.SortExpression;
                if (salesOrderQuery.SortDirection == "ASC")
                {
                    salesOrderQuery.SortDirection = "DESC";
                }
                else if (salesOrderQuery.SortDirection == "DESC")
                {
                    salesOrderQuery.SortDirection = "ASC";
                }
                this.BindData2SalesOrderList(salesOrderQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void lnkbtnSalesOrderSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvSalesOrder, "ORDER_ID");
                List<string> listStatus = base.GetSelectedIDsInGridView(this.gvSalesOrder, "STATUS");
                if ((listStatus != null) || (listStatus.Count != 0))
                {
                    for (int i = 0; i < listStatus.Count; i++)
                    {
                        if (listStatus[i].ToString().ToUpper() != "NEW")
                        {
                            this.lblMsg.Text = "提交失败:单据状态不为新建,无法提交!";
                            return;
                        }
                    }
                }
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C008");
                }
                else
                {
                    string shippingOrderID = OrderMgtServiceClientProxy.GetInstance().ConvertSalesOrder2ShippingOrder(selectedID);
                    this.lblMsg.Text = shippingOrderID;
                    SalesOrderQueryEntity salesOrderQuery = this.GetSalesOrderQueryEntity();
                    this.BindData2SalesOrderList(salesOrderQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = "提交失败:" + ex.Message;
            }
        }

        private void LoadData()
        {
            this.ViewState["PriorityFlag"] = base.GetValueFromAppSettings("PriorityFlag");
            SalesOrderQueryEntity salesOrderQuery = new SalesOrderQueryEntity();
            if ((string.IsNullOrEmpty(base.Request.QueryString["from"]) || (base.Request.QueryString["from"] == "query")) || (base.Request.QueryString["from"] == "list"))
            {
                if (this.PreviousPage != null)
                {
                    salesOrderQuery.OrderID = this.PreviousPage.SoID;
                    salesOrderQuery.OwnerID = this.PreviousPage.OwnerID;
                    salesOrderQuery.ConsigneeID = this.PreviousPage.CustomerID;
                    salesOrderQuery.OrderDate = this.PreviousPage.SoDate;
                    salesOrderQuery.RequestedDeliveryDate = this.PreviousPage.ExpectArrivalDate;
                    salesOrderQuery.Type = this.PreviousPage.OrderType;
                    salesOrderQuery.ExternOrderID = this.PreviousPage.ExternalOrderID;
                    salesOrderQuery.Status = this.PreviousPage.OrderStatus;
                    salesOrderQuery.CreatedBy = this.PreviousPage.CreatedBy;
                }
                else
                {
                    salesOrderQuery.OrderID = "";
                    salesOrderQuery.OwnerID = "";
                    salesOrderQuery.ConsigneeID = "";
                    salesOrderQuery.OrderDate = "";
                    salesOrderQuery.RequestedDeliveryDate = "";
                    salesOrderQuery.Type = "";
                    salesOrderQuery.ExternOrderID = "";
                    salesOrderQuery.Status = "";
                    salesOrderQuery.CreatedBy = base.GetParamsString(base.UserInfo.UserID, "Same As");
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if ((this.hdnFrom.Value.ToLower().Trim() != "edit") || !string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                salesOrderQuery.IsGetAll = false;
                salesOrderQuery.CurrentPage = "0";
                salesOrderQuery.PageSize = this.gvSalesOrder.PageSize.ToString();
                salesOrderQuery.SortField = "ORDER_ID";
                salesOrderQuery.SortDirection = "DESC";
                this.PagerFooter.PageSize = this.gvSalesOrder.PageSize;
                if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
                {
                    this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                    this.btnFinish.Visible = false;
                    this.gvSalesOrder.Columns[1].Visible = false;
                    this.gvSalesOrder.Columns[2].Visible = true;
                    this.btnNew.Visible = false;
                    this.btnEdit.Visible = false;
                    this.btnDelete.Visible = false;
                    this.btnAction.Visible = false;
                    this.btnReSearch.Visible = false;
                    this.btnExport.Visible = false;
                    this.btnReport.Visible = false;
                }
                if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
                {
                    this.hdnActionID.Value = base.Request.QueryString["export"];
                }
                this.SetSalesOrderQueryEntity(salesOrderQuery);
            }
            else
            {
                salesOrderQuery = this.GetSalesOrderQueryEntity();
            }
            this.BindData2SalesOrderList(salesOrderQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetSalesOrderQueryEntity(SalesOrderQueryEntity salesOrderQuery)
        {
            base.PageQueryEntitySession = salesOrderQuery;
        }

        public OnLineSO2Query PreviousPage
        {
            get
            {
                return (OnLineSO2Query) base.PreviousPage;
            }
        }
    }
}

